package compiler;

/**
 * The Class MyParseException.
 * 
 */
public class ItaParseException extends ParseException {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Instantiates a new my parse exception.
	 * 
	 * @param e
	 *            the e
	 */
	public ItaParseException(ParseException e) {
		super(
				initialise(e.currentToken, e.expectedTokenSequences,
						e.tokenImage));
	}

	/**
	 * initialize() uses "currentToken" and "expectedTokenSequences" to generate
	 * a parse error message and returns it. If this object has been created due
	 * to a parse error, and you do not catch it (it gets thrown from the
	 * parser) the correct error message gets displayed.
	 * 
	 * @param currentToken
	 *            the current token
	 * @param expectedTokenSequences
	 *            the expected token sequences
	 * @param tokenImage
	 *            the token image
	 * @return the string
	 */
	public static String initialise(Token currentToken,
			int[][] expectedTokenSequences, String[] tokenImage) {
		StringBuffer expected = new StringBuffer();
		int maxSize = 0;
		for (int i = 0; i < expectedTokenSequences.length; i++) {
			if (maxSize < expectedTokenSequences[i].length) {
				maxSize = expectedTokenSequences[i].length;
			}

			for (int j = 0; j < expectedTokenSequences[i].length; j++) {
				if (i > 0)
					expected.append(',').append(
							tokenImage[expectedTokenSequences[i][j]]);
				else
					expected.append(tokenImage[expectedTokenSequences[i][j]]);
			}

		}
		String retval = "Ho incontrato \"";
		Token tok = currentToken.next;
		for (int i = 0; i < maxSize; i++) {
			if (i != 0)
				retval += " ";
			if (tok.kind == 0) {
				retval += tokenImage[0];
				break;
			}
			retval += " ";

			retval += add_escapes(tok.image);
			retval += " \"";
			tok = tok.next;
		}
		if (expectedTokenSequences.length == 1) {
			retval += " mentre aspettavo ";
		} else {
			retval += " mentre aspettavo uno tra ";
		}
		retval += expected.toString();
		return retval;
	}
}
